<?php

/**
 * If you are not authorized to modify this file, do NOT touch it.
 * @file auth.func.php
 * @author deltamaster
 * @date 2011-4-16
 * @description The function library for user authentication.
 */

if (!defined('IN_IMBA')) exit('Access Denied.');


/**
 * @name CheckAuthenticationAvailability
 * @author deltamaster
 * @description Check whether the basic authentication feature is enabled.
 * @parameter void
 * @return bool : true if authentication feature available, false if authentication feature is disabled or no sufficient priviliges.
 * @data 2011-4-18
 */
function CheckAuthenticationAvailability()
{
	global $db;
	if (!IMBA_AUTH_ENABLED) return false;
	$result = $db->query("SELECT * FROM {$db->tablepre}" . IMBA_AUTH_TABLE . " WHERE FALSE");
	if ($result) return true;
	else
	{
		$result = $db->query("CREATE TABLE IF NOT EXISTS `{$db->tablepre}" . IMBA_AUTH_TABLE . "` (
  `uid` int(11) NOT NULL auto_increment,
  `username` varchar(20) NOT NULL,
  `password` char(32) NOT NULL,
  `email` varchar(80) NOT NULL,
  `regdate` timestamp NOT NULL default CURRENT_TIMESTAMP,
  `activated` tinyint(1) NOT NULL default '1',
  `enabled` tinyint(1) NOT NULL default '1',
  PRIMARY KEY  (`uid`),
  UNIQUE KEY `username` (`username`,`email`)
) ENGINE=MyISAM AUTO_INCREMENT=1 ;");
		$result = $db->query("SELECT * FROM {$db->tablepre}" . IMBA_AUTH_TABLE . " WHERE FALSE");
		if ($result) return true;
		else return false;
	}
}

function AddUser($username, $password, $email, $activated = 1, $enabled = 1)
{
    global $db;
    if (!CheckAuthenticationAvailability()) return false;
    $password = md5($password);
    $result = $db->query("INSERT INTO {$db->tablepre}" . IMBA_AUTH_TABLE . " (username, password, email, activated, enabled) VALUES ('{$username}', '{$password}', '{$email}', '{$activated}', '{$enabled}')");
    if ($result) return true;
    else return false;
}

?>